package com.joshua.viewtest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.joshua.viewtest.demo.BaiduMapOfflineDemo.OfflineMapAty;
import com.joshua.viewtest.demo.ElevationAndShadow.ElevationAndShadowAty;
import com.joshua.viewtest.demo.ExpandingView.ExpandingTestAty;
import com.joshua.viewtest.demo.OtherActivity;
import com.joshua.viewtest.demo.RecylerView.RecyclerViewAty;
import com.joshua.viewtest.demo.TopToolBarTest.TopToolBarAty;
import com.joshua.viewtest.demo.cardview.CardViewTestAty;
import com.joshua.viewtest.demo.coordinatorlayout.CoordinatorActivity;
import com.joshua.viewtest.demo.floatingtoolbar.FloatingToolbarAty;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static List<Item> list = new ArrayList<>();
    private static List<Class<? extends Activity>> classList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        DisplayUtil.initData(this);

        initData();
        final ListView listView = (ListView) findViewById(android.R.id.list);
        CustomAdapter adapter = new CustomAdapter(MainActivity.this,
                R.layout.item_main_list, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(MainActivity.this, classList.get(position)));
            }
        });
    }

    private void initData() {
        list.add(new Item("CoordinatorLayout", "Material风格的重要组件, 协调(Coordinate)其他组件, 实现联动。"));
        classList.add(CoordinatorActivity.class);
        list.add(new Item("RecyclerView", "通过使用RecyclerView控件，我们可以在APP中创建带有Material " +
                "Design风格的复杂列表。"));
        classList.add(RecyclerViewAty.class);
        list.add(new Item("FloatingToolbar", "这是一个由FloatingActionButton变成一个工具栏的Android UI " +
                "控件。动画效果优雅，遵循Material Design风格。"));
        classList.add(FloatingToolbarAty.class);
        list.add(new Item("ElevationAndShadow", "视图和阴影\n" +
                "View的大小位置都是通过x，y确定的，而现在有了z轴的概念，而这个z值就是View" +
                "的高度（elevation），而高度决定了阴影（shadow）的大小。"));
        classList.add(ElevationAndShadowAty.class);
        list.add(new Item("CardView", "CardView是一个卡片布局，布局可以包含圆角和阴影，本质上CardView是一个FrameLayout" +
                "，因此它作为一个布局容器，可以布局其他的View。"));
        classList.add(CardViewTestAty.class);
        list.add(new Item("TopToolBar",
                "Toolbar是应用的内容的标准工具栏，可以说是Actionbar的升级版，两者不是独立关系，要使用Toolbar还是得跟ActionBar" +
                        "扯上关系的。"));
        classList.add(TopToolBarAty.class);
        list.add(new Item("ExpandTabView Test", "Item可展开ListView"));
        classList.add(ExpandingTestAty.class);
        list.add(new Item("BaiduOfflineMap Demo", "百度离线地图管理"));
        classList.add(OfflineMapAty.class);
        list.add(new Item("Other", "其他"));
        classList.add(OtherActivity.class);
    }

    class CustomAdapter extends ArrayAdapter {
        private LayoutInflater mInflater;
        private int mResource;

        public CustomAdapter(Context context, int resource, List<Item> objects) {
            super(context, resource, objects);
            mInflater = LayoutInflater.from(context);
            mResource = resource;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Item data = (Item) getItem(position);
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = mInflater.inflate(mResource, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.setTitle(data.getName());
            viewHolder.setDescription(data.getDescription());
            return convertView;
        }
    }

    class ViewHolder {
        private TextView title;
        private TextView description;

        public ViewHolder(View view) {
            title = (TextView) view.findViewById(R.id.item_title);
            description = (TextView) view.findViewById(R.id.item_content);
        }

        public void setTitle(String data) {
            title.setText(data);
        }

        public void setDescription(String data) {
            description.setText(data);
        }
    }

    class Item {
        private String name;
        private String description;

        public Item(String name, String description) {
            this.name = name;
            this.description = description;
        }

        public String getName() {
            return name;
        }

        public String getDescription() {
            return description;
        }
    }
}


